package com.xk5156.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: xk
 * @date: 2021.11.26
 * @version: 1.0
 * @description: 水波扩散
 */
public class WaveView extends View {
  /**
   * 中心圆paint
   */
  private Paint centerPaint;
  /**
   * 中心圆半径
   */
  private float radius;
  /**
   * 扩散圆paint
   */
  private Paint spreadPaint;
  /**
   * 圆心x
   */
  private float centerX;
  /**
   * 圆心y
   */
  private float centerY;
  /**
   * 每次圆递增间距
   */
  private float distance;
  /***
   * 最大圆半径
   */
  private float maxRadius;
  /**
   * 中心圆颜色
   */
  private int centerColor;
  /**
   * 扩散圆颜色
   */
  private int spreadColor;
  /**
   * 扩散圆初始透明度
   */
  private int centerAlpha;
  /**
   * 圆数量
   */
  private int maxCircle;
  /**
   * 扩散延迟间隔，越大扩散越慢
   */
  private int delayMilliseconds;
  /**
   * 扩散圆层级数，元素为扩散的距离
   */
  private List<Integer> spreadRadius = new ArrayList<>();
  /**
   * 对应每层圆的透明度
   */
  private List<Integer> alphas = new ArrayList<>();

  public WaveView(Context context) {
    this(context, null, 0);
  }
  public WaveView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveView, defStyleAttr, 0);
    radius = typedArray.getDimension(R.styleable.WaveView_wave_radius, 100);
    maxRadius = typedArray.getDimension(R.styleable.WaveView_wave_max_radius, 300);
    centerColor = typedArray.getColor(R.styleable.WaveView_wave_center_color, Color.BLUE);
    spreadColor = typedArray.getColor(R.styleable.WaveView_wave_spread_color, Color.BLUE);
    distance = typedArray.getDimension(R.styleable.WaveView_wave_distance, 10);
    centerAlpha =typedArray.getInteger(R.styleable.WaveView_wave_center_alpha,255);
    maxCircle =typedArray.getInteger(R.styleable.WaveView_wave_max_circle,20);
    delayMilliseconds =typedArray.getInteger(R.styleable.WaveView_wave_delay_milliseconds,33);
    typedArray.recycle();
    centerPaint = new Paint();
    centerPaint.setColor(centerColor);
    centerPaint.setAntiAlias(true);
    //最开始不透明且扩散距离为0
    alphas.add(centerAlpha);
    spreadRadius.add(0);
    spreadPaint = new Paint();
    spreadPaint.setAntiAlias(true);
    spreadPaint.setAlpha(centerAlpha);
    spreadPaint.setColor(spreadColor);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.TRANSPARENT);
    for (int i = 0; i < spreadRadius.size(); i++) {
      int alpha = alphas.get(i);
      spreadPaint.setAlpha(alpha);
      int width = spreadRadius.get(i);
      //绘制扩散的圆
      canvas.drawCircle(centerX, centerY, radius + width, spreadPaint);
      Log.e("===="," w:"+getWidth()+" w1:"+width+"  radius:"+radius+"  distance:"+distance+"  i:"+i);
      //每次扩散圆半径递增，圆透明度递减
      if (alpha > 0 && width < maxRadius) {
        alpha = alpha - (centerAlpha/maxCircle) > 0 ? alpha - (centerAlpha/maxCircle) : 0;
        alphas.set(i, alpha);
        spreadRadius.set(i, (int) (width + distance));
        Log.d("====","  set i:"+(width+distance));
      }
    }
    //当最外层扩散圆半径达到最大半径时添加新扩散圆
    if (spreadRadius.get(spreadRadius.size() - 1) >= maxRadius/3) {
      spreadRadius.add(0);
      alphas.add(centerAlpha);
      Log.d("====","  大于maxRadius:"+maxRadius);
    }else {
      Log.d("====","  小于maxRadius:"+maxRadius);
    }
    if (spreadRadius.size() >= maxCircle) {
      alphas.remove(0);
      spreadRadius.remove(0);
    }
    //中间的圆
    canvas.drawCircle(centerX, centerY, radius, centerPaint);
    //延迟更新，达到扩散视觉差效果
    postInvalidateDelayed(delayMilliseconds);
  }


  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    //圆心位置
    centerX = w / 2;
    centerY = h / 2;
  }
}
